package com.trium.commercial;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

import com.trium.commercial.R;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.ClipData;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity {

    // Navigation drawer
    DrawerLayout drawer;
    ActionBarDrawerToggle drawerToggle;

    /**
     * Called when the activity is first created.
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        ajouterDrawer();


        findViewById(R.id.listView).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());

        findViewById(R.id.droite).setOnDragListener(new MyDragListener());
        findViewById(R.id.gauche).setOnDragListener(new MyDragListener());

    }

    public void ajouterDrawer(){

        //ImageView view = (ImageView) findViewById(android.R.id.home);
        //view.setPadding(10, 0, 7, 0);
        //getActionBar().setIcon(R.drawable.ic_inapp);
        getActionBar().setDisplayShowHomeEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        findViewById(R.id.left_drawer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        drawerToggle = new ActionBarDrawerToggle(this, drawer,
                R.drawable.apptheme_ic_navigation_drawer, R.string.app_name, R.string.app_name) {

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }
        };

        // On modifie les propriétés du drawer
        drawer.setDrawerShadow(getResources().getDrawable(R.drawable.drawer_shadow), Gravity.START);
        drawer.setScrimColor(getResources().getColor(R.color.blanc));
        drawer.setDrawerListener(drawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }


    private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            } else {
                return false;
            }
        }
    }

    class MyDragListener implements OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {

            View view = (View) event.getLocalState();

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    // do nothing
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:

                    break;
                case DragEvent.ACTION_DRAG_EXITED:

                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    ViewGroup container = (ViewGroup) v;

                    if(view.getId() == R.id.listView){
                        ListView lv = new ListView(getApplicationContext());

                        String[] strings = new String[]{
                                "njdjnd","njdjnd","njdjnd","njdjnd","njdjnd","njdjnd","njdjnd","njdjnd","njdjnd","njdjnd"
                        };

                        lv.setAdapter(new ArrayAdapter<String> ( getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, strings ));
                        ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,1);

                        container.addView(lv);

                        animerApparition(lv);
                    }else {
                        container.addView(view);
                        //view.setVisibility(View.VISIBLE);
                    }
                    //Toast.makeText(getApplicationContext(),"Droped",Toast.LENGTH_SHORT).show();
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    view.setVisibility(View.VISIBLE);

                default:
                    break;
            }
            return true;
        }
    }

    public void animerApparition(View view){
        AnimatorSet anim = new AnimatorSet();
        anim.setDuration(500);
        anim.playTogether(
                ObjectAnimator.ofFloat(view,"scaleX",0,1),
                ObjectAnimator.ofFloat(view,"scaleY",0,1)
        );

        anim.start();
    }
}
